package cn.sfrog.project.biz.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.PageResult;
import cn.sfrog.common.constant.Constants;
import cn.sfrog.common.core.controller.BaseController;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.page.TableDataInfo;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.bean.BeanUtils;
import cn.sfrog.project.biz.admin.service.*;
import cn.sfrog.project.biz.web.vo.index.*;
import cn.sfrog.system.domain.SysNotice;
import cn.sfrog.system.domain.vo.app.AppEscortQueryVO;
import cn.sfrog.system.domain.vo.app.AppEscortResultVO;
import cn.sfrog.system.domain.vo.app.AppServiceResultVO;
import cn.sfrog.system.service.ISysNoticeService;
import cn.sfrog.system.service.ISysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api/index")
@Api(value = "/api/index", tags = "APP首页相关")
public class AppIndexController extends BaseController {

    @Autowired
    private IBizBannerService bizBannerService;
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Autowired
    private IBizDoctorService bizDoctorService;
    @Autowired
    private IBizHospitalService bizHospitalService;
    @Autowired
    private IBizDepartmentService bizDepartmentService;
    @Autowired
    private IBizSearchKeywordService bizSearchKeywordService;
    @Autowired
    private IBizSearchKeywordUserHistoryService bizSearchKeywordUserHistoryService;
    @Autowired
    private ISysUserService sysUserService;

    @ApiOperation(value = "首页全局搜索")
    @GetMapping("/getIndexGlobalSearch")
    public HashMap getIndexGlobalSearch(@ApiParam(name = "keyword",value = "关键词") @RequestParam(value = "keyword",required = false) String keyword,
                                           @ApiParam(name = "pageNum",value = "关键词") @RequestParam(value = "pageNum",required = false) Integer pageNum,
                                           @ApiParam(name = "pageSize",value = "关键词") @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                                                 @ApiParam(name = "cityCode",value = "城市code") @RequestParam(value = "cityCode",required = true) String cityCode,
                                                                 @ApiParam(name = "type",value = "类型【1=名院 2=名医 3=陪护 4=服务】") @RequestParam(value = "type",required = true) String type){
        AppGlobalSearchVO appGlobalSearchVO = bizHospitalService.selectAppIndexGlobalSearch(keyword,cityCode,type);
        HashMap map = new HashMap<>();
        map.put("hospitalCount",appGlobalSearchVO.getHospitalCount());
        map.put("doctorCount",appGlobalSearchVO.getDoctorCount());
        map.put("escortCount",appGlobalSearchVO.getEscortCount());
        map.put("serviceCount",appGlobalSearchVO.getServiceCount());
        PageHelper.startPage(pageNum, pageSize); // 每页的大小为pageSize，查询第page页的结果
        if("1".equals(type)){
            List<AppHospitalResultVO> page = CollUtil.page(pageNum, pageSize, appGlobalSearchVO.getHospitalList());
            map.put("pageIndex", pageNum); // 当前页
            map.put("pageSize", pageSize); // 每页条数
            map.put("doctorTotal", appGlobalSearchVO.getDoctorCount()); // 总记录数
            map.put("pages", Math.ceil(appGlobalSearchVO.getDoctorCount()/pageSize));// 总页数
            map.put("list", page); // 数据列表
            return map;
        }else if("2".equals(type)){
            List<AppDoctorDetailResultVO> page = CollUtil.page(pageNum, pageSize, appGlobalSearchVO.getDoctorList());
            map.put("pageIndex", pageNum); // 当前页
            map.put("pageSize", pageSize); // 每页条数
            map.put("doctorTotal", appGlobalSearchVO.getDoctorCount()); // 总记录数
            map.put("pages", Math.ceil(appGlobalSearchVO.getDoctorCount()/pageSize));// 总页数
            map.put("list", page); // 数据列表
            return map;
        }else if("3".equals(type)){
            List<AppEscortResultVO> page = CollUtil.page(pageNum, pageSize, appGlobalSearchVO.getEscortList());
            map.put("pageIndex", pageNum); // 当前页
            map.put("pageSize", pageSize); // 每页条数
            map.put("doctorTotal", appGlobalSearchVO.getEscortCount()); // 总记录数
            map.put("pages", Math.ceil(appGlobalSearchVO.getEscortCount()/pageSize));// 总页数
            map.put("list", page); // 数据列表
            return map;
        }else if("4".equals(type)){
            List<AppServiceResultVO> page = CollUtil.page(pageNum, pageSize, appGlobalSearchVO.getServiceList());
            map.put("pageIndex", pageNum); // 当前页
            map.put("pageSize", pageSize); // 每页条数
            map.put("doctorTotal", appGlobalSearchVO.getServiceCount()); // 总记录数
            map.put("pages", Math.ceil(appGlobalSearchVO.getServiceCount()/pageSize));// 总页数
            map.put("list", page); // 数据列表
        }
        return null;
    }


    @ApiOperation(value = "获取首页热搜关键词")
    @GetMapping("/getIndexHotSearchKeyword")
    public ApiAjaxResult getIndexHotSearchKeyword(){
        List<String> keywordList = bizSearchKeywordService.selectAppIndexHotSearchKeyword();
        return ApiAjaxResult.success(keywordList);
    }

    @ApiOperation(value = "获取用户搜索历史")
    @GetMapping("/getUserSearchHistory")
    public ApiAjaxResult getUserSearchHistory(){
        List<String> keywordList = bizSearchKeywordUserHistoryService.selectAppUserSearchHistory();
        return ApiAjaxResult.success(keywordList);
    }

    @ApiOperation(value = "删除用户搜索历史")
    @PostMapping("/deleteUserSearchHistory")
    public ApiAjaxResult deleteUserSearchHistory(){
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return ApiAjaxResult.success(bizSearchKeywordUserHistoryService.deleteUserSearchHistory(userId));
    }

    @ApiOperation(value = "获取首页轮播图列表")
    @GetMapping("/getIndexBannerList")
    public ApiAjaxResult<List<AppBannerResultVO>> getIndexBannerList(){
        List<AppBannerResultVO> bannerResultVOList = bizBannerService.selectAppIndexBannerList();
        return ApiAjaxResult.success(bannerResultVOList);
    }


    @ApiOperation(value = "获取通知公告列表")
    @GetMapping("/getIndexNoticeList")
    public ApiAjaxResult<List<AppNoticeResultVO>> getIndexNoticeList(){
        SysNotice sysNotice = new SysNotice();
        sysNotice.setStatus(Constants.STATUS_NORMAL);
        List<SysNotice> sysNotices = sysNoticeService.selectNoticeList(sysNotice);
        List<AppNoticeResultVO> appNoticeResultVOS = BeanUtils.convertList2List(sysNotices, AppNoticeResultVO.class);
        return ApiAjaxResult.success(appNoticeResultVOS);
    }

    @ApiOperation(value = "获取首页名医推荐")
    @GetMapping("/getIndexDoctorList")
    public ApiAjaxResult<List<AppDoctorResultVO>> getIndexDoctorList(AppDoctorQueryVO queryVO){
        List<AppDoctorResultVO> doctorResultVOList = bizDoctorService.selectAppIndexDoctorList(queryVO);
        return ApiAjaxResult.success(doctorResultVOList);
    }

    @ApiOperation(value = "获取首页热门医院")
    @GetMapping("/getIndexHospitalList")
    public ApiAjaxResult<List<AppHospitalResultVO>> getIndexHospitalList(AppHospitalQueryVO queryVO){
        List<AppHospitalResultVO> hospitalResultVOList = bizHospitalService.selectAppIndexHospitalList(queryVO);
        return ApiAjaxResult.success(hospitalResultVOList);
    }

    @ApiOperation(value = "更多医院列表")
    @GetMapping("/getAllHospitalList")
    public TableDataInfo<List<AppHospitalResultVO>> getAllHospitalList(AppHospitalQueryVO queryVO){
        startPage();
        List<AppHospitalResultVO> hospitalResultVOList = bizHospitalService.selectAllHospitalList(queryVO);
        System.out.println(hospitalResultVOList);
        return getDataTable(hospitalResultVOList);
    }

    @ApiOperation(value = "不分页更多医院列表")
    @GetMapping("/getAllHospital")
    public ApiAjaxResult<List<AppHospitalResultVO>> getAllHospitalListNoPage(AppHospitalQueryVO queryVO){
        List<AppHospitalResultVO> hospitalResultVOList = bizHospitalService.selectAllHospitalList(queryVO);
        return ApiAjaxResult.success(hospitalResultVOList);
    }

    @ApiOperation(value = "获取医院详情")
    @GetMapping("/getHospitalDetail/{id}")
    public ApiAjaxResult<AppHospitalDetailResultVO> getHospitalDetail(@PathVariable Long id){
        AppHospitalDetailResultVO detailResultVO = bizHospitalService.selectAppHospitalDetail(id);
        return ApiAjaxResult.success(detailResultVO);
    }

    @ApiOperation(value = "获取科室详情")
    @GetMapping("/getDepartmentDetail/{id}")
    public ApiAjaxResult<AppDepartmentDetailResultVO> getDepartmentDetail(@PathVariable("id") Long id){
        AppDepartmentDetailResultVO detailResultVO = bizDepartmentService.selectAppDepartmentDetail(id);
        return ApiAjaxResult.success(detailResultVO);
    }

    @ApiOperation(value = "获取医生详情")
    @GetMapping("/getDoctorDetail/{id}")
    public ApiAjaxResult<AppDoctorDetailResultVO> getDoctorDetail(@PathVariable("id") Long id){
        AppDoctorDetailResultVO resultVO =  bizDoctorService.selectAppDoctorDetail(id);
        return ApiAjaxResult.success(resultVO);
    }

    @ApiOperation(value = "获取所有科室列表")
    @GetMapping("/getAllDepartmentList")
    public ApiAjaxResult<List<AppDepartmentNameResultVO>> getAllDepartmentList(){
//        List<AppDepartmentNameResultVO> departmentNameResultVOS = bizDepartmentService.selectAppAllDepartmentList();
//        return ApiAjaxResult.success(departmentNameResultVOS);

        List<AppDepartmentNameResultVO> departmentNameResultVOS = new ArrayList<>();
        AppDepartmentNameResultVO appDepartmentNameResultVO = new AppDepartmentNameResultVO();
        appDepartmentNameResultVO.setDepartmentName("~此软件版本已不适用，请重新在应用商店更新下载~");
        departmentNameResultVOS.add(appDepartmentNameResultVO);
        return ApiAjaxResult.success(departmentNameResultVOS);
    }

    @ApiOperation(value = "获取所有科室列表")
    @GetMapping("/getAllDepartmentList/{cityCode}")
    public ApiAjaxResult<List<AppDepartmentNameResultVO>> getAllDepartmentList(@PathVariable("cityCode") String cityCode){
        List<AppDepartmentNameResultVO> departmentNameResultVOS = bizDepartmentService.selectAppAllDepartmentListByCityCode(cityCode);
        return ApiAjaxResult.success(departmentNameResultVOS);
    }

    @ApiOperation(value = "获取名医列表")
    @GetMapping("/getFamousDoctorList")
    public TableDataInfo<List<AppDoctorDetailResultVO>> getFamousDoctorList(AppDoctorQueryVO queryVO){
        startPage();
        List<AppDoctorDetailResultVO> resultVOList = bizDoctorService.selectAppFamousDoctorList(queryVO);
        return getDataTable(resultVOList);
    }

    @ApiOperation(value = "获取首页热门陪护列表")
    @GetMapping("/getIndexHotEscortList")
    public ApiAjaxResult getIndexHotEscortList(AppEscortQueryVO queryVO){
        List<AppEscortResultVO> escortResultVOList = sysUserService.getAppIndexHotEscortList(queryVO);
        return ApiAjaxResult.success(escortResultVOList);
    }

    @ApiOperation(value = "名医模块中根据医院Id获取一级科室")
    @GetMapping("/getFirstDepartmentList/{hospitalId}")
    public ApiAjaxResult<List<AppFirstDepartmentNameResultVO>> getFirstDepartmentList(@PathVariable("hospitalId")
                                                                                        Long hospitalId){
        List<AppFirstDepartmentNameResultVO> appFirstDepartmentNameResultVOS = bizDepartmentService.getFirstDepartmentList(hospitalId);
        return ApiAjaxResult.success(appFirstDepartmentNameResultVOS);
    }

    @ApiOperation(value="名医模块获取二级科室列表")
    @GetMapping("/getSecondDepartmentList")
    public ApiAjaxResult<List<AppDepartmentNameResultVO>> getSecondDepartmentList(AppDepartmentParentQueryVO queryVO){
        List<AppDepartmentNameResultVO> secondDepartmentList = bizDepartmentService.getSecondDepartmentList(queryVO);
        return ApiAjaxResult.success(secondDepartmentList);
    }
    @ApiOperation(value="名医模块获取医生列表")
    @GetMapping("/getAllHospitalDoctorList")
    public TableDataInfo<List<AppDoctorDetailResultVO>> selectAllHospitalDoctorList(AppDoctorManyQueryVO queryVO){
        startPage();
        List<AppDoctorDetailResultVO> appDoctorDetailResultVOS = bizDoctorService.selectAllHospitalDoctorList(queryVO);
        return getDataTable(appDoctorDetailResultVOS);
    }


}
